/// @tags:
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

using ull = unsigned long long;

void print(ull x) {
  for (int i = 0; i < 64; ++i) {
    cout << (x >> i & 1);
  }
  cout << endl;
}

int main() {
  int t;
  cin >> t;
  while (t--) {
    ull n, x;
    cin >> n >> x;
    ull ans = 0, sup = UINT64_MAX, low = n;
    bool flag = true;
    for (int i = 0; i < 64; ++i) {
      int nb = n >> i & 1ull;
      int xb = x >> i & 1ull;
      if (nb == 1) {
        if (xb == 1) {
          sup = std::min(sup, n | ((1ull << i) - 1ull));
        } else {
          // cout << "test:";
          // print((n + (1 << i)));
          low = std::max(low, ((n + (1ull << (i))) >> i) << i);
        }
      } else {
        if (xb == 1) {
          flag = false;
        }
      }
    }
    if (sup >= low) {
      ans = low;
    } else {
      flag = false;
    }
    if (flag) {
      // print(n);
      // print(x);
      // print(ans);
      cout << ans << '\n';
    } else {
      cout << -1 << '\n';
    }
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("C. Interesting Sequence.in", "r", stdin);
  freopen("C. Interesting Sequence.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
